package com.leo.ktvplayer.pay.alipay;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.leo.ktvplayer.base.App;
import com.leo.ktvplayer.localcookie.LocalUtils;
import com.leo.ktvplayer.pay.BasePaymentThread;
import com.leo.ktvplayer.util.JsonUtil;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class Alipayment extends BasePaymentThread {
    /**
     * 支付宝的ID
     */
    public static String APPID = "2017010604891789";
    /**
     * 支付宝的私钥
     */
    public static String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMumBydEpTyXBE7CIFxtHn/BgUxxxarpJZILKaSWxkzI0w8T4x9eqUajonyW3uljlgNrZPH8q4nA02tSN3kAwU5LEXiy2RUT4dWMPCm7w88SZ5hklYx/KkypF7ZWO8hBWubuW/nQ5ClrV8B8UspUgCeSCCZTBz62ZP8aAfAqkw3pAgMBAAECgYB+1xsvfxb3xQu+zjR/vMs5QVn6Ngaq6+olq2Q1il9aZd7nTjixSjFeuP60YPfYH9nIqXRHMFvbQcms15mLeUfsEhPZKR+KiAY3hm6IBHh0S3PLzrp9+vTWs7Seed8HxewiftNoIC5+SR+pRrJMBK2AnmG5PLZtaiQbjbPT9a0ajQJBAPA6Iiy/NyyR1X4M3bR0r+VWSZnNMFS4M40ju/brqgK4xRV1yVOInTfa80SoAPsDirfevu+XYCUyYIBAyt8bd7MCQQDZBRAJoXRXyFE0qR0Pg/+/7C2n8S1m6uDkvgKFtfImP/3keiYoHDuoUj8TbrzjWHe5oPyUnVgJvAHDC1JnNlXzAkAbsHez9K5lYsHa75y2aiTEKHf/VFlXyouWnyHRSkSkX6vH13XHPR3yDDiyFzNVu4jHvNMBRs98OhxJbC52HPJlAkEArTCiHN1xaICfdcQkQfaYikRkJyuMjR6SvVl5z9Vk2uukpcppWwc1AN4qIkr0iQZX/ZchG0T6NkKIsWv+pgmxywJACMuplfx+We/69mrUV8zshws7NCMYYiTZk2T354L2lPb0HFGB9CJt6p0FlYDrMmj2w1mvkpnApUxf9lC46vNizg==";

    /**
     * 装数据用的map
     */
    private Map<String, String> map = new HashMap<String, String>();
    /**
     * PAY的URL
     */
    private static String ALIPAY_URL = "https://openapi.alipay.com/gateway.do";

    private void configMap() {
        map.put("app_id", LocalUtils.getUser().getAlipay_appid()); //应用的APPID
        map.put("charset", "UTF-8"); //设置编码
        map.put("sign_type", "RSA"); //设置签名类型
        map.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        map.put("version", "1.0");;
        map.put("method", "alipay.trade.precreate");
        map.put("biz_content", "{"
                + "    \"out_trade_no\":\""+ orderId+"\","
                + "    \"total_amount\":\""+ money +"\","
                + "    \"subject\":\"kty娱乐系统\","
                + "    \"store_id\":\""+ "001"+"\","
                + "    \"timeout_express\":\"1200m\"}");
        try {
            map.put("sign", AlipaySignature.rsaSign(map,LocalUtils.getUser().getAlipay_key(),"UTF-8"));
            String s = HttpsUtil.doGet(ALIPAY_URL, map);
            Log.d("acthome","->" + s);
            AliPaymentRespone respone = JsonUtil.jsonStrToObj(AliPaymentRespone.class,s);

            Message message = Message.obtain();
            message.obj = respone.alipay_trade_precreate_response.qr_code;
            message.what = ON_URL_CALL;
            handler.sendMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean configQueryMap()throws Exception {
        map.clear();
        map.put("app_id", APPID);
        map.put("charset", "UTF-8");
        map.put("sign_type", "RSA");
        map.put("timestamp", "2016-12-30 03:07:50");
        map.put("version", "1.0");
        map.put("method", "alipay.trade.query");
        map.put("biz_content", "{"
                + "    \"out_trade_no\":\""+orderId+"\"}");

        map.put("sign", AlipaySignature.rsaSign(map, PRIVATE_KEY, "UTF-8"));
        String result = HttpsUtil.doGet("https://openapi.alipay.com/gateway.do", map);
        if (result.contains("TRADE_SUCCESS")) {
            handler.sendEmptyMessage(ON_SUCCESS);
            return false;
        }
        return true;
    }

    @Override
    public synchronized void start() {
        super.start();

    }

    @Override
    public void run() {
        super.run();
        Log.d("acthome","------->" );
        configMap();

        try {
            Thread.sleep(2000);
            while (isrunning) {
                isrunning = configQueryMap();
                Thread.sleep(2000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 第一层内容
     */
    class AliPaymentRespone{
        /**
         * 实际内容
         */
        private AliPaymentContent alipay_trade_precreate_response;
        /**
         * 响应签名
         */
        private String sign;
    }

    /**
     * 实际内容
     */
    class AliPaymentContent{
        /**
         * 响应吗
         */
        private String code;
        /**
         * 相应信息
         */
        private String msg;
        /**
         * 相应二维码
         */
        private String qr_code;
        /**
         * 相应订单号
         */
        private String out_trade_no;

    }
}
